<template>
    <el-container class="home-container">
        <el-header class="herder" height="70px">
            <div>
                <i class="el-icon-menu collapse-btn" @click="toggleCollapse"></i>
            </div>
            <div>
                <span>music后台管理</span>
            </div>
            <div class="header_right">
                <div class="btn-fullscreen" @click="handleFullScreen">
                    <el-tooltip placement="bottom" :content="fullscreen?'取消全屏':'全屏'">
                        <i class="el-icon-rank"></i>
                    </el-tooltip>
                </div>
                <div class="user-avator">
                    <img src="../assets/img/user.jpg"/>
                </div>
                <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                         <span class="el-dropdown-link">
                             <span class="user-name-span">
                                 {{userName}}
                             </span>
                             <i class="el-icon-caret-bottom"></i>
                         </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </el-header>
        <el-container>
            <el-aside :width="collapse ?'64px':'200px'" class="sidebar">
                <!--侧边栏菜单区域-->
                <el-menu class="sidebar-el-menu"
                         :default-active="onRoutes"
                         :collapse="collapse"
                         :collapse-transition="false"
                         background-color="#334256"
                         text-color="#ffffff"
                         active-text-color="#20a0ff"
                         router>
                    <el-submenu index="1">
                        <template slot="title">
                            <i class="el-icon-s-home"></i>
                            <!--文本-->
                            <span>系统管理</span>
                        </template>
                        <template v-for="item in items">
                            <template>
                                <el-menu-item :index="item.index" :key="item.index">
                                    <i :class="item.icon"></i>
                                    <span> {{item.title}}</span>
                                </el-menu-item>
                            </template>
                        </template>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
    export default {
        data() {
            return {
                collapse: false, //折叠
                fullscreen: false,
                items: [
                    {
                        icon: 'el-icon-document',
                        index: '/Info',
                        title: '系统首页'
                    },
                    {
                        icon: 'el-icon-s-custom',
                        index: '/Consumer',
                        title: '用户管理'
                    },
                    {
                        icon: 'el-icon-document',
                        index: '/Singer',
                        title: '歌手管理'
                    },
                    {
                        icon: 'el-icon-document',
                        index: '/SongList',
                        title: '歌单管理'
                    }
                ]
            }
        },
        methods: {
            // 点击按钮,切换侧边栏折叠
            toggleCollapse() {
                this.collapse = !this.collapse
            },
            //全屏事件
            handleFullScreen() {
                if (this.fullscreen) {
                    if (document.exitFullscreen) {
                        document.exitFullscreen()
                    } else if (document.webkitCancelFullScreen) {      //safari 、Chrome
                        document.webkitCancelFullScreen()
                    } else if (document.mozCancelFullScreen) {        //firefox
                        document.mozCancelFullScreen()
                    } else if (document.msExitFullScreen) {            //ie
                        document.msExitFullScreen()
                    }
                } else {
                    let element = document.documentElement
                    if (element.requestFullscreen) {
                        element.requestFullscreen()
                    } else if (element.webkitRequestFullScreen) {      //safari 、Chrome
                        element.webkitRequestFullScreen()
                    } else if (element.mozRequestFullScreen) {         //firefox
                        element.mozRequestFullScreen()
                    } else if (element.msRequestFullScreen) {         //ie
                        element.msRequestFullScreen()
                    }
                }
                this.fullscreen = !this.fullscreen
            },
            handleCommand(command) {
                if (command == 'logout') {
                    localStorage.removeItem('userName')
                    this.$router.push('/')
                }
            }
        },
        computed: {
            onRoutes() {
                return '/' + this.$route.path.replace('/', '')
                //return '/Info'
            },
            userName() {
                return localStorage.getItem('userName')
            }
        }
    }
</script>
<style scoped>

    .home-container {
        height: 100%;
    }

    .herder {
        background-color: #253041;
        box-sizing: border-box;
        font-size: 22px;
        color: #FFF;
        line-height: 70px;
    }

    .herder > div:nth-child(-n+2) {
        float: left;
        margin-right: 12px;
    }


    .sidebar {
        background-color: #334256;
        border-right: none;
    }

    .el-menu {
        width: 100%;
    }

    .header_right {
        float: right;
        padding-right: 50px;
        display: flex;
        height: 70px;
        align-items: center;
    }

    .btn-fullscreen {
        margin-right: 5px;
        font-size: 24px;
        transform: rotate(45deg);
        cursor: pointer;
    }

    .user-avator {
        margin-left: 20px;
    }

    .user-avator img {
        display: block;
        width: 40px;
        height: 40px;
        border-radius: 50%;
    }

    .user-name {
        margin-left: 10px;
    }

    .el-dropdown-link {
        color: #ffffff;
        cursor: pointer;
        display: block;
    }

    /*限制文本长度*/
    .user-name-span {
        font-size: 14px;
        float: left;
        display: block;
        width: 80px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }
</style>